package com.ibm.framework.exception.concurrent;

import com.ibm.framework.exception.handler.ExceptionInfoExtractor;
import com.ibm.framework.exception.handler.ExceptionInfoFormater;
import com.ibm.framework.exception.logger.ExceptionLogger;
import com.ibm.framework.exception.logger.LogMessageBuilder;
import com.ibm.framework.exception.model.ExceptionInfo;
import com.ibm.framework.exception.model.RawExceptionInfo;

/**
 * 异常写日志文件处理工作线程，调用ExceptionHandler来处理异常信息。
 * 
 * @author matrixgan
 * 
 */

public class LogExceptionProcessWorker implements Runnable {
    /**
     * 异常信息
     */
    private RawExceptionInfo rawEx;
    /**
     * 异常信息提取器
     */
    private ExceptionInfoExtractor exceptionInfoExtractor;
    /**
     * 异常信息格式化工具
     */
    private ExceptionInfoFormater exceptionInfoFormater;

    /**
     * 构造方法
     * 
     * @param rawEx 异常信息
     * @param exceptionInfoExtractor 异常信息抽取器
     * @param exceptionInfoFormater 异常信息格式化工具
     */
    public LogExceptionProcessWorker(RawExceptionInfo rawEx, ExceptionInfoExtractor exceptionInfoExtractor,
            ExceptionInfoFormater exceptionInfoFormater) {
        this.rawEx = rawEx;
        this.exceptionInfoExtractor = exceptionInfoExtractor;
        this.exceptionInfoFormater = exceptionInfoFormater;
    }

    /**
     * 运行
     */
    public void run() {
        try {
            ExceptionInfo exceptionInfo = exceptionInfoExtractor.extract(rawEx);
            String formatMes = exceptionInfoFormater.getFormatString(exceptionInfo);
            ExceptionLogger.log(formatMes);
        } catch (Exception e) {
            String msg = LogMessageBuilder.build("Fail to handle the intercepted exception!", e);
            ExceptionLogger.log(msg);
        }
    }
}
